package pers.vic.practice.base.controller;

import org.springframework.beans.factory.annotation.Autowired;
import pers.vic.boot.base.controller.BaseConsoleController;
import pers.vic.boot.base.tool.Tools;
import pers.vic.practice.base.constant.BaseConstant;
import pers.vic.practice.base.model.Principal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @description: base controller for student score
 * @author Vic.xu
 * @date: 2021/8/20 0020 16:58
 */
public class BaseController extends BaseConsoleController {

    @Autowired
    protected HttpServletResponse response;

    @Autowired
    protected HttpServletRequest request;

    public static String PRINCIPAL = BaseConstant.USER_SESSION_KEY;

    /**
     * 当前用户
     * 
     * @return
     */
    public static Principal currentUser() {
        HttpSession httpSession = Tools.currentSession();
        if (httpSession == null) {
            return null;
        }
        Principal user = (Principal)Tools.currentSession().getAttribute(PRINCIPAL);
        return user;
    }

    /**
     * 当前用户id
     * 
     * @return
     */
    public int currentUserId() {
        Principal user = currentUser();
        if (user == null) {
            return 0;
        }
        return 0;
    }


    /** 保存用户到session */
    public static void storeUser(Principal user) {
        Tools.currentRequest().getSession().setAttribute(PRINCIPAL, user);

    }

    /** 清除session */
    public static void cleanSession() {
        HttpSession httpSession = Tools.currentSession();
        if (httpSession == null) {
            return;
        }
        httpSession.setAttribute(PRINCIPAL, null);
        httpSession.invalidate();
    }

}
